package com.example.controller;

import com.example.pojo.CreditScoreLog;
import com.example.pojo.TakersJob;
import com.example.pojo.User;
import com.example.pojo.UserCreditScore;
import com.example.pojo.VO.TakeJobUser;
import com.example.service.CreditScoreLogService;
import com.example.service.TakersJobService;
import com.example.service.UserCreditScoreService;
import com.example.service.UserService;
import com.example.util.ResponseWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/jobTaker")
public class JobTakerController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserCreditScoreService creditScoreService;

    @Autowired
    private TakersJobService takersJobService;

    @Autowired
    private CreditScoreLogService creditScoreLogService;


    @GetMapping("/getTakerUsers")
    public ResponseWrapper<List<TakeJobUser>> getTakeJobUsers(@RequestParam(required = false) List<Integer> takersId,@RequestParam Integer jobId){
        if (takersId == null || takersId.isEmpty()) {
            return ResponseWrapper.success(Collections.emptyList());
        }

        List<User> userList = userService.getUserListById(takersId);
        if (userList == null || userList.isEmpty()) {
            return ResponseWrapper.success(Collections.emptyList());
        }

        List<Integer> userIdList = new ArrayList<>();
        List<TakeJobUser> takeJobUserList = new ArrayList<>();
        for (User user : userList) {
            TakeJobUser takeJobUser = new TakeJobUser();
            BeanUtils.copyProperties(user, takeJobUser);
            takeJobUser.setUserId(user.getIndetifier()); // id映射
            takeJobUser.setPhone(user.getTel());         // tel映射
            takeJobUserList.add(takeJobUser);
            userIdList.add(user.getIndetifier());
        }

        List<UserCreditScore> userCreditScoreList = creditScoreService.getCreditScoreListByUserId(takersId);
        if (userCreditScoreList != null && !userCreditScoreList.isEmpty()) {
            Map<Integer, UserCreditScore> creditMap = userCreditScoreList.stream()
                    .collect(Collectors.toMap(UserCreditScore::getUserId, cs -> cs));

            for (TakeJobUser takeJobUser : takeJobUserList) {
                UserCreditScore score = creditMap.get(takeJobUser.getUserId());
                if (score != null) {
                    takeJobUser.setCreditScore(score.getCreditScore());
                    takeJobUser.setLevel(score.getLevel());
                }
            }
        }

        List<TakersJob> takersJobList = takersJobService.getJobPeople(userIdList,jobId);
        Map<Integer, TakersJob> jobMap = takersJobList.stream().collect(Collectors.toMap(TakersJob::getUserId, job -> job));

        for (TakeJobUser user : takeJobUserList) {
            TakersJob job = jobMap.get(user.getUserId());
            if (job != null) {
                user.setIsReceive(
                        job.getAccess() == 0 ? "未处理" : job.getAccess() == 1 ? "通过" : "拒绝"
                );
            }
        }
        System.out.println(takeJobUserList);
        return ResponseWrapper.success(takeJobUserList);
    }

    @GetMapping("/receivePeopleNum/{jobId}")
    public ResponseWrapper<Integer> getReceivePeopleNum(@PathVariable Integer jobId){
        Integer receiveJobNum = takersJobService.getReceiveJobNumByJobId(jobId);
        return ResponseWrapper.success(receiveJobNum);
    }

    @GetMapping("/receivePeople/{jobId}/{userId}/{type}")
    public ResponseWrapper receivePeople(@PathVariable Integer jobId,
                                                  @PathVariable Integer userId,@PathVariable Integer type){
        takersJobService.receiveOrRejectOne(jobId,userId,type);
        return ResponseWrapper.success();
    }

    @GetMapping("/creditLog/{userId}")
    public ResponseWrapper<List<CreditScoreLog>> getUserCreditLog(@PathVariable Integer userId){
        List<CreditScoreLog> creditScoreLogList = creditScoreLogService.getUserCreditLog(userId);
        return ResponseWrapper.success(creditScoreLogList);
    }

}
